package com.cloud.wms.ui.inside.revert;

import android.app.Activity;
import android.content.Intent;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cloud.core.eventBus.Event;
import com.cloud.core.eventBus.EventBusUtil;
import com.cloud.core.utils.NumberUtils;
import com.cloud.wms.R;
import com.cloud.wms.base.BaseBarListActivity;
import com.cloud.wms.config.EventConfig;
import com.cloud.wms.config.IntentKey;
import com.cloud.wms.ui.inside.contract.RevertContact;
import com.cloud.wms.ui.inside.entity.RevertBatchEntity;
import com.cloud.wms.ui.inside.entity.RevertEntity;
import com.cloud.wms.ui.inside.presenter.RevertPresenter;
import com.cloud.wms.ui.main.entity.CommodityEntity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * @Author:Chen
 * @Date: 2021-03-19 10:17
 * @Description:
 */
public class RevertBatchActivity extends BaseBarListActivity<RevertBatchEntity, RevertPresenter> implements RevertContact.View {
    @BindView(R.id.tv_commodity_code)
    TextView tvCommodityCode;
    @BindView(R.id.tv_commodity_name)
    TextView tvCommodityName;
    @BindView(R.id.tv_pos_code)
    TextView tvPosCode;
    @BindView(R.id.tv_commodity_amount)
    TextView tvCommodityAmount;
    @BindView(R.id.et_commodity_amount)
    EditText etCommodityAmount;
    @BindView(R.id.btn_submit)
    TextView btnSubmit;
    private RevertEntity revertBatch;
    private CommodityEntity commodityEntity;
    private int returnType;

    @Override
    protected String barTitle() {
        return "还货批次";
    }

    @Override
    protected int getItemResId() {
        return R.layout.item_inside_revert_batch;
    }

    @Override
    protected void initEvent() {
        super.initEvent();
        rvList.requestFocus();
        btnSubmit.setEnabled(true);
        etCommodityAmount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                int amount = NumberUtils.getIntegerValue(s.toString());
                if (amount > 0 && amount <= revertBatch.getTotalAmount()) {
                    btnSubmit.setEnabled(true);
                } else {
                    btnSubmit.setEnabled(false);
                }
            }
        });

        commodityEntity = (CommodityEntity) getIntent().getParcelableExtra(IntentKey.INFO_KEY);
        returnType = getIntent().getIntExtra(IntentKey.TYPE_KEY, 1);
        tvCommodityCode.setText(commodityEntity.getBarCode());
        tvCommodityName.setText(commodityEntity.getCommodityName());
        tvPosCode.setText(commodityEntity.getPosCode());

        revertBatch = getIntent().getParcelableExtra(IntentKey.OBJECT_KEY);
        mAdapter.setNewInstance(revertBatch.getBatchList());
    }

    @Override
    protected void bindItemData(BaseViewHolder helper, RevertBatchEntity item) {
        helper.setText(R.id.tv_produce_date, item.getProductionDate().length() > 11 ? item.getProductionDate().substring(0, 11) : item.getProductionDate())
                .setText(R.id.tv_deadline_date, item.getExpirationDate().length() > 11 ? item.getExpirationDate().substring(0, 11) : item.getExpirationDate())
                .setText(R.id.tv_in_date, item.getInDate().length() > 11 ? item.getInDate().substring(0, 11) : item.getInDate())
                .setTextColorRes(R.id.tv_produce_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_produce_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setTextColorRes(R.id.tv_deadline_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_deadline_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setTextColorRes(R.id.tv_in_date, item.isSelect() ? R.color.font_white : R.color.font_black)
                .setTextColorRes(R.id.tv_in_date_name, item.isSelect() ? R.color.font_white : R.color.font_gray)
                .setBackgroundResource(R.id.ll_batch, item.isSelect() ? R.drawable.bg_item_common_select : R.drawable.bg_item_common);
    }

    @Override
    protected void itemClickListener(BaseQuickAdapter<RevertBatchEntity, BaseViewHolder> mAdapter, View view, int position) {
        mAdapter.getData().get(position).setSelect(!mAdapter.getData().get(position).isSelect());
        int amount = 0;
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            RevertBatchEntity entity = mAdapter.getData().get(i);
            if (entity.isSelect()) {
                amount += entity.getAmount();
            }
        }
        if (amount > 0) {
            tvCommodityAmount.setText(amount + "");
            etCommodityAmount.setText("1");
            etCommodityAmount.setSelection(etCommodityAmount.getText().length());
        } else {
            tvCommodityAmount.setText("");
            etCommodityAmount.setText("");
        }
        mAdapter.notifyDataSetChanged();
        btnSubmit.setEnabled(amount > 0);
    }

    @Override
    protected int getResId() {
        return R.layout.activity_inside_revert_confirm;
    }

    @Override
    protected void setupComponent() {
        getActivityComponent().inject(this);
    }

    @Override
    public void onSuccess(Message message) {
        switch (message.what) {
            case RevertPresenter.SUBMIT_REVERT_AMOUNT:
                success("还货成功！");
                EventBusUtil.sendEvent(new Event<>(EventConfig.CLOSE_REFRESH));
                this.finish();
                break;
        }
    }

    @OnClick(R.id.btn_submit)
    public void onViewClicked() {
        int amount = NumberUtils.getIntegerValue(etCommodityAmount.getText().toString());
        if (amount < 0) {
            info("请输入大于0的数量");
            return;
        }
        if (amount > revertBatch.getTotalAmount()) {
            info("超出最大还货数量！");
            return;
        }
        List<RevertBatchEntity> selectBatchList = new ArrayList<>();
        for (int i = 0; i < mAdapter.getData().size(); i++) {
            if (mAdapter.getItem(i).isSelect()) {
                selectBatchList.add(mAdapter.getItem(i));
                if (amount > mAdapter.getItem(i).getAmount())
                    amount = amount - mAdapter.getItem(i).getAmount();
                else
                    mAdapter.getItem(i).setAmount(amount);
            }
        }
        mPresenter.confirmUpShelf(commodityEntity.getCommodityId(), String.valueOf(commodityEntity.getPosID()), amount, returnType, selectBatchList);
    }

    public static void start(Activity activity, CommodityEntity commodityBean, RevertEntity revertBatch, int type) {
        Intent intent = new Intent(activity, RevertBatchActivity.class);
        intent.putExtra(IntentKey.INFO_KEY, commodityBean);
        intent.putExtra(IntentKey.OBJECT_KEY, revertBatch);
        activity.startActivity(intent);
    }
}
